package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author Mr_Su
 * @version v1.0
 * @date 2022/7/5 15:12
 * @desciption
 */
@Component
@Slf4j
public class UpOrDownListener {

    @Autowired
    private IApArticleService apArticleService;

    @KafkaListener(topics = "${topic.upDownTopic}")
    public void upOrDown(ConsumerRecord<String, String> record) {
        String value = record.value();
        if (StringUtils.isNotBlank(value)) {
            Map map = JSON.parseObject(value, Map.class);
            Long articleId = (Long) map.get("articleId");
            Boolean enable = (Boolean) map.get("enable");

            ApArticle apArticle = apArticleService.getById(articleId);
            if (apArticle != null) {
                apArticle.setIsDown(!enable);
                apArticleService.updateById(apArticle);
                log.info("app文章上下架完成.......");
            }
        }
    }
}
